/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.anhndt.audiobook.ui.renderer;

import com.anhndt.audiobook.business.CustomBorder;
import com.anhndt.audiobook.business.MediaPlayerControl;
import com.anhndt.audiobook.entity.ItemPartEntity;
import com.anhndt.audiobook.utils.Util;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Graphics;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.list.ListCellRenderer;
import com.sun.lwuit.plaf.UIManager;

/**
 *
 * @author Sun
 */
public class ItemArticlePartRenderer extends Container implements ListCellRenderer {

    Label name = new Label();
    Label duration = new Label();

    /**
     * @author Sun
     */
    public ItemArticlePartRenderer() {
        setUIID("ListRenderer");
        setFocusable(true);
        setLayout(new BorderLayout());
        int color = UIManager.getInstance().getComponentStyle("Line").getBgColor();
        getUnselectedStyle().setBorder(new CustomBorder(color, 1, CustomBorder.BOTTOM));
        getSelectedStyle().setBorder(getUnselectedStyle().getBorder());
        name.setUIID("Name");
        duration.setUIID("Timer");
        Container panelPrice = new Container(new BorderLayout());

        Container panelInfo = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        panelInfo.addComponent(name);
        panelInfo.addComponent(panelPrice);
        addComponent(BorderLayout.CENTER, panelInfo);
        addComponent(BorderLayout.EAST, duration);
//        getUnselectedStyle().setBorder(Border.getDefaultBorder());
//        getSelectedStyle().setBorder(Border.getDefaultBorder());
    }

    public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {
        setFocus(isSelected);
        ItemPartEntity item = (ItemPartEntity) value;
        boolean isSelectedInfo = (list != null && list.getComponentForm().getFocused() == list) ? isSelected : false;
        name.setFocus(isSelectedInfo);
        if (item.isMarked()) {
            int fgColor = getSelectedStyle().getFgColor();
            name.getUnselectedStyle().setFgColor(fgColor);
        } else {
            int fgColor = UIManager.getInstance().getComponentStyle("Name").getFgColor();
            name.getUnselectedStyle().setFgColor(fgColor);
            name.setFocus(isSelected);
        }
        name.setText(item.name);
        if (item.isPlaying()) {
            duration.setText(null);
            duration.setIcon(Util.getImage("playing"));
        } else if (item.isPaused()) {
            duration.setText(null);
            duration.setIcon(Util.getImage("paused"));
        } else {
            duration.setText(MediaPlayerControl.formatTime(item.getDuration()));
            duration.setIcon(null);
        }
        return this;
    }

    public Component getListFocusComponent(List list) {
        return null;
    }
}
